import pygame
from pygame.locals import  *
import sys
import time
import random

WINDOW_WIDTH = 650
WINDOW_HEIGHT = 650

DEFAULT_FPS = 60
DEFAULT_DELAY = 1.0/DEFAULT_FPS -0.002

def collide(r1,r2):
    """
    碰撞判断
    """
    if r1.x > r2.x + r2.width:
        return False
    if r1.x + r1.width < r2.x:
        return False
    if r1.y > r2.y + r2.height:
        return False
    if r1.y + r1.height < r2.y:
        return False
    return True

#对方车辆
class BYD:
    def __init__(self,window):
        self.window = window
        self.reset()

    def move(self):
        self.y += 5
        if self.y > WINDOW_HEIGHT:
            self.reset()

    def display(self):
        self.window.blit(self.img,(self.x,self.y))

    def reset(self):
        self.img = pygame.image.load("UFO.png")

        self.width = self.img.get_width()
        self.height = self.img.get_height()

        #车道
        byd_left =  (WINDOW_WIDTH/2-self.width)/2
        byd_right = WINDOW_WIDTH*3/4 -self.width/2
        self.x =random.choice([byd_left,byd_right])
        self.y = - self.height/2

class TESLA:
    def __init__(self,window):
        self.window = window
        self.img = pygame.image.load("Police.png")

        self.width = self.img.get_width()
        self.height = self.img.get_height()

        #车道
        tesla_left =  (WINDOW_WIDTH/2-self.width)/2
        tesla_right = WINDOW_WIDTH*3/4 -self.width/2

        self.x =random.choice([tesla_left,tesla_right])
        self.y =WINDOW_HEIGHT- self.height  + 16

    def display(self):
        self.window.blit(self.img,(self.x,self.y))

    #雷达探测 距离对方车距离  
    def leida(self, parameter_list):
        pass

    #驾驶左右车道    
    def driveLeft(self):
        self.x = (WINDOW_WIDTH/2-self.width)/2
    def driveRight(self):
        self.x = WINDOW_WIDTH*3/4 -self.width/2


if __name__ == "__main__":
    pygame.init()

    window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
    pygame.display.set_caption("AI小车")

    fps = 0
    font = pygame.font.Font("happy.ttf", 24)

    score = 0 

    bg = pygame.image.load("bg.jpg")
    

    #2车道显示
    pygame.draw.line(window,(0x33, 0xCC, 0x33),(325,0),(325,768),2)

    
    font_finish = pygame.font.Font("happy.ttf", 48)
    finish_text = font_finish.render("游戏结束", True, (0xff, 0, 0))
    ft_width = finish_text.get_width()
    ft_height = finish_text.get_height()
    ft_x = (WINDOW_WIDTH - ft_width) / 2
    ft_y = (WINDOW_HEIGHT - ft_height) / 2 - 50

    #我要开车了
    tesla = TESLA(window)

    #对方车来了
    byd = BYD(window)

    is_over = False

    while True:
        start = time.time()

        window.blit(bg,(0,0))
        pygame.draw.line(window,(0x33, 0xCC, 0x33),(325,0),(325,768),2)
        
        fps_text = font.render("FPS:%d"%fps,True,(0x33, 0xCC, 0x33))
        window.blit(fps_text,(540,10))

        score_text = font.render("积分: %d" % score, True, (0x33, 0xCC, 0x33))
        window.blit(score_text, (10, 10))

        if is_over:
            window.blit(finish_text,(ft_x,ft_y))

        if not is_over:
            #显示汽车
            tesla.display()

            byd.display()
            byd.move()

            tesla_rect = pygame.Rect(tesla.x,tesla.y,tesla.width,tesla.height)
            byd_rect = pygame.Rect(byd.x,byd.y,byd.width,byd.height)

            if collide(tesla_rect,byd_rect):
                is_over = True
            score +=10
        #刷新图像
        pygame.display.flip()

        ###########事件捕获
        events = pygame.event.get()
        for event in events:
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            if event.type  == K_RETURN and is_over:
                #重置状态
                is_over = False

        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            tesla.driveLeft()
        if keys[K_RIGHT]:
            tesla.driveRight()

        end = time.time()
        cost = end- start

        if cost< DEFAULT_DELAY:
            sleep = DEFAULT_DELAY-cost
        else:
            sleep = 0
        
        time.sleep(sleep)
        end = time.time()
        fps = 1.0/(end-start)





        
